package com.ctosb.classviewer.clazz.attr;

import com.ctosb.classviewer.ClassReader;
import com.ctosb.classviewer.clazz.cp.CONSTANT_Class_info;
import com.ctosb.classviewer.clazz.cp.CONSTANT_Utf8_info;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class Exceptions_attribute extends Attribute_info {

    private short number_of_exceptions;
    private short exception_index_table[];

    @Override
    protected void doParse(ClassReader classReader) {
        this.number_of_exceptions = classReader.readShort();
        exception_index_table = new short[number_of_exceptions & 0xffff];
        for (int i = 0; i < exception_index_table.length; i++) {
            exception_index_table[i] = classReader.readShort();
        }
    }

    /**
     * 获取异常表信息
     * @Author alan
     * @Date 2020/12/16 12:15 上午
     * @return
     */
    public CONSTANT_Class_info[] getExceptions() {
        CONSTANT_Class_info[] constant_class_infos= new CONSTANT_Class_info[number_of_exceptions];
        for (int i = 0; i < exception_index_table.length; i++) {
            constant_class_infos[i] = (CONSTANT_Class_info) getClassFile().getConstant_pool()[exception_index_table[i]];
        }
        return constant_class_infos;
    }
}
